package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.PhysicalCardChangeLogEntity;
import city.spring.modules.cardinfo.service.PhysicalCardChangeLogService;
import city.spring.modules.cardinfo.vo.PhysicalCardChangeLogEntityVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：物理卡号更换记录
 *
 * @author pxm
 * @date 2020-08-17 11:34:59
 */
@RestController
@RequestMapping("/physical_card_change_log")
public class PhysicalCardChangeLogController extends ApiController {
    private final PhysicalCardChangeLogService physicalCardChangeLogService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<PhysicalCardChangeLogEntity, ?>> entityOrderFields;

    public PhysicalCardChangeLogController(PhysicalCardChangeLogService physicalCardChangeLogService) {
        this.physicalCardChangeLogService = physicalCardChangeLogService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(PhysicalCardChangeLogEntity::getId);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getPersonId);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getPersonType);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getOldCard);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getNewCard);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getReplaceTime);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getCounts);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getGmtCreate);
        entityOrderFields.add(PhysicalCardChangeLogEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>物理卡号更换记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, PhysicalCardChangeLogEntity entity) {
        LambdaQueryChainWrapper<PhysicalCardChangeLogEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>物理卡号更换记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping("old")
    public Object list(@PageableDefault Pageable pageable, PhysicalCardChangeLogEntityVO entity) {
        QueryWrapper<? extends PhysicalCardChangeLogEntity> queryWrapper = buildQuery(entity);
        return success(physicalCardChangeLogService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }


    /**
     * 分页获取 <strong>物理卡号更换记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping("staff")
    public Object staffList(@PageableDefault Pageable pageable, PhysicalCardChangeLogEntityVO entity) {
        QueryWrapper<? extends PhysicalCardChangeLogEntity> queryWrapper = staffBuildQuery(entity);
        return success(physicalCardChangeLogService.staffListPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>物理卡号更换记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<PhysicalCardChangeLogEntityVO> page = new Page<>();
        QueryWrapper<PhysicalCardChangeLogEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<PhysicalCardChangeLogEntityVO> iPage = physicalCardChangeLogService.listPage(page, wrapper);
        List<PhysicalCardChangeLogEntityVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>物理卡号更换记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody PhysicalCardChangeLogEntity entity) {
        physicalCardChangeLogService.savePhysicalCardChangeLog(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>物理卡号更换记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody PhysicalCardChangeLogEntity entity) {
        entity.setId(id);
        physicalCardChangeLogService.updatePhysicalCardChangeLog(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>物理卡号更换记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        physicalCardChangeLogService.deletePhysicalCardChangeLog(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>物理卡号更换记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        physicalCardChangeLogService.deletePhysicalCardChangeLog(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<PhysicalCardChangeLogEntity> buildLambdaQuery(PhysicalCardChangeLogEntity entity) {
        LambdaQueryChainWrapper<PhysicalCardChangeLogEntity> lambdaQuery = physicalCardChangeLogService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), PhysicalCardChangeLogEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonId()), PhysicalCardChangeLogEntity::getPersonId, entity.getPersonId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldCard()), PhysicalCardChangeLogEntity::getOldCard, entity.getOldCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getNewCard()), PhysicalCardChangeLogEntity::getNewCard, entity.getNewCard());
        lambdaQuery.eq(entity.getReplaceTime() != null, PhysicalCardChangeLogEntity::getReplaceTime, entity.getReplaceTime());
        lambdaQuery.eq(entity.getGmtCreate() != null, PhysicalCardChangeLogEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, PhysicalCardChangeLogEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends PhysicalCardChangeLogEntity> buildQuery(PhysicalCardChangeLogEntityVO entity) {
        QueryWrapper<? extends PhysicalCardChangeLogEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonId()), "t1.person_id", entity.getPersonId());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonType()), "t1.person_type", entity.getPersonType());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getOldCard()), "t1.old_card", entity.getOldCard());
        wrapper.like(StringUtils.isNotBlank(entity.getNewCard()), "t1.new_card", entity.getNewCard());
        wrapper.eq(entity.getReplaceTime() != null, "t1.replace_time", entity.getReplaceTime());
        wrapper.eq(entity.getCounts() != null, "t1.counts", entity.getCounts());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        return wrapper;
    }

    /**
     * 构建查询条件内容(员工)
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends PhysicalCardChangeLogEntity> staffBuildQuery(PhysicalCardChangeLogEntityVO entity) {
        QueryWrapper<? extends PhysicalCardChangeLogEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonId()), "t1.person_id", entity.getPersonId());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonType()), "t1.person_type", entity.getPersonType());
        wrapper.like(StringUtils.isNotBlank(entity.getStaffName()), "t2.name", entity.getStaffName());
        wrapper.like(StringUtils.isNotBlank(entity.getOldCard()), "t1.old_card", entity.getOldCard());
        wrapper.like(StringUtils.isNotBlank(entity.getNewCard()), "t1.new_card", entity.getNewCard());
        wrapper.eq(entity.getReplaceTime() != null, "t1.replace_time", entity.getReplaceTime());
        wrapper.eq(entity.getCounts() != null, "t1.counts", entity.getCounts());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
